
package cn.org.seventh.common;

import java.util.Locale;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Nov 30, 2007 4:15:50 PM
 */
public final class LocaleUtil {

    public static Locale getLocale(String lang, String defaultLang) {
        Locale locale = getLocale(lang, false);
        if (locale == null) {
            locale = getLocale(defaultLang, true);
        }
        return locale;
    }

    public static Locale getLocale(String lang) {
        return getLocale(lang, true);
    }

    public static Locale getLocale(String lang, boolean getSystemDefaultOnFail) {
        if (StringUtil.isEmpty(lang)) {
            if (getSystemDefaultOnFail) {
                return Locale.getDefault();
            }
            return null;
        }
        int i = lang.indexOf('_');
        if (i != -1) {
            String l = lang.substring(0, i);
            String c = lang.substring(i + 1);
            return new Locale(l, c);
        }
        return new Locale(lang);
    }
}
